package com.nextwist.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.activation.DataHandler;

import javax.ws.rs.core.Response;

import org.apache.cxf.jaxrs.ext.multipart.Attachment;

import com.nexttwist.common.BaseException;
import com.nexttwist.common.Type;
import com.nexttwist.common.Util;
import com.nexttwist.pojo.Flag;
import com.nexttwist.pojo.Line;
import com.nexttwist.pojo.Story;
import com.nexttwist.pojo.Vote;
import com.nextwist.dao.StoryDAO;
import com.nextwist.interfaces.IStoryInterface;

public class StoryImpl implements IStoryInterface {
	StoryDAO storyDAO = new StoryDAO();

	@Override
	public Response addStory(Story storyObject) {
		try {
			return Util.successResponse(storyDAO.createStory(storyObject));
		} catch (BaseException e) {
			return Util.failureResponse(e.getMessage());
		} catch (Exception e) {
			return Util.failureResponse(e.getMessage());
		}
	}

	@Override
	public Response updateStory(Story storyObject) {
		try {
			return Util.successResponse(storyDAO.editStory(storyObject));
		} catch (BaseException e) {
			return Util.failureResponse(e.getMessage());
		} catch (Exception e) {
			return Util.failureResponse(e.getMessage());
		}
	}

	@Override
	public Response deleteStory(int storyId) {
		try {
			return Util.successResponse(storyDAO.deleteStory(storyId));
		} catch (BaseException e) {
			return Util.failureResponse(e.getMessage());
		} catch (Exception e) {
			return Util.failureResponse(e.getMessage());
		}
	}

	@Override
	public Response getEntryLine(int storyId, int storyLineId, int userId) {
		try {
			return Util.successResponse(storyDAO.getEntryLine(storyId,
					storyLineId, userId));
		} catch (BaseException e) {
			return Util.failureResponse(e.getMessage());
		} catch (Exception e) {
			return Util.failureResponse(e.getMessage());
		}
	}

	@Override
	public Response getStoryLine(int storyId) {
		try {
			return Util.successResponse(storyDAO.getStoryLine(storyId));
		} catch (BaseException e) {
			return Util.failureResponse(e.getMessage());
		} catch (Exception e) {
			return Util.failureResponse(e.getMessage());
		}
	}

	@Override
	public Response addLine(Line lineObject) {
		try {
			return Util.successResponse(storyDAO.insertLine(lineObject));
		} catch (BaseException e) {
			return Util.failureResponse(e.getMessage());
		} catch (Exception e) {
			return Util.failureResponse(e.getMessage());
		}
	}

	@Override
	public Response deleteLine(int storyId, int uniqueId) {
		try {
			return Util.successResponse(storyDAO.deleteLine(storyId, uniqueId));
		} catch (BaseException e) {
			return Util.failureResponse(e.getMessage());
		} catch (Exception e) {
			return Util.failureResponse(e.getMessage());
		}
	}

	@Override
	public Response addFlag(Flag flagObject) {
		try {
			return Util.successResponse(storyDAO.reportFlag(flagObject));
		} catch (BaseException e) {
			return Util.failureResponse(e.getMessage());
		} catch (Exception e) {
			return Util.failureResponse(e.getMessage());
		}
	}

	@Override
	public Response giveVote(Vote voteObj) {
		try {
			return Util.successResponse(storyDAO.giveVote(voteObj));
		} catch (BaseException e) {
			return Util.failureResponse(e.getMessage());
		} catch (Exception e) {
			return Util.failureResponse(e.getMessage());
		}
	}

	@Override
	public Response searchStory(String searchLine) {
		try {
			return Util.successResponse(storyDAO.searchStory(searchLine));
		} catch (BaseException e) {
			return Util.failureResponse(e.getMessage());
		} catch (Exception e) {
			return Util.failureResponse(e.getMessage());
		}
	}

	@Override
	public Response addSong(int userId, String title, Attachment attachment) {
		try {
			String fileName = uploadFile(attachment);

			Story story = new Story();
			story.setStoryTitle(title);
			story.setStoryLine(fileName);
			story.setUserId(userId);
			story.setType(Type.audio);
			return Util.successResponse(storyDAO.createStory(story));
		} catch (Exception e) {
			return Util.failureResponse(e.getMessage());
		}
	}

	@Override
	public Response addSongLine(int userId, int storyId, Attachment attachment) {
		try {
			String fileName = uploadFile(attachment);

			Line lineObj = new Line();
			lineObj.setStoryId(storyId);
			lineObj.setUserId(userId);
			lineObj.setNewline(fileName);
			lineObj.setType(Type.audio);
			return Util.successResponse(storyDAO.insertLine(lineObj));
		} catch (Exception e) {
			return Util.failureResponse(e.getMessage());
		}
	}

	/**
	 * This method is used to get the file name from the header
	 * 
	 * @param fileName
	 * @return
	 */
	private String getFileName(String fileName) {

		String[] contentDisposition = fileName.split(";");

		for (String filename : contentDisposition) {
			if ((filename.trim().startsWith("filename"))) {

				String[] name = filename.split("=");

				String finalFileName = name[1].trim().replaceAll("\"", "");
				return finalFileName;
			}
		}
		return "unknown";
	}

	/**
	 * This method is used to upload file in server
	 * 
	 * @param attachment
	 * @return
	 * @throws Exception
	 */
	private String uploadFile(Attachment attachment) throws Exception {
		DataHandler handler = attachment.getDataHandler();
		InputStream stream = handler.getInputStream();
		String fileDetails = attachment.getHeader("Content-Disposition");
		String fileName = getFileName(fileDetails);
		OutputStream out = new FileOutputStream(new File("D:\\nextwist svn\\"
				+ fileName));

		int read = 0;
		byte[] bytes = new byte[1024];
		while ((read = stream.read(bytes)) != -1) {
			out.write(bytes, 0, read);
		}
		stream.close();
		out.flush();

		out.close();
		return fileName;
	}

	@Override
	public Response getHotStories() {
		try {
			return Util.successResponse(storyDAO.findHotStories());
		} catch (Exception e) {
			return Util.failureResponse(e.getMessage());
		}
	}

	@Override
	public Response getFreshStories() {
		try {
			return Util.successResponse(storyDAO.getFreshStories());
		} catch (Exception e) {
			return Util.failureResponse(e.getMessage());
		}
	}
}
